package path;
import geom.Geom;
import geom.PolarVector;
import geom.Vector;

import java.util.ArrayList;
import java.util.Iterator;

import path.Path;
import path.PathDrawer;
import processing.core.PApplet;
import shapes.Cross;
import shapes.PolyCircle;
import util.RndGen;

public class DrawParticleStrings implements PathDrawer{
	public static float PI = (float) Math.PI;
	RndGen rnd;
	

	ArrayList strings;
	
	public DrawParticleStrings(int amount, int age){
		rnd = new RndGen();
		strings = new ArrayList();
		for(int i = 0; i < amount; i++)
			strings.add(new ParticleString(2, i%2, age));
		
		
	}

	public void drawPath(Path path, PApplet pa) {
		if(path.getSize() <= 0) return;
		Vector p = path.getLastKeyFrame().getPosition();
		Iterator si = strings.iterator();
		while(si.hasNext()){
			ParticleString ps = (ParticleString)si.next();
			ps.tick(p);
			ps.draw(pa);
		}
	}

	public void keyAdded(Path p) {
		// TODO Auto-generated method stub
		
	}

	public void keysAdded(Path p, int size) {
		// TODO Auto-generated method stub
		
	}

	public void keyRemoved(Path p, int i) {
		// TODO Auto-generated method stub
		
	}


	
	private class ParticleString{
		ArrayList string;
		int birthInterval;
		int tick;
		int maxAge;
		
		public ParticleString(int interval, int offset, int maxAge){
			this.birthInterval = interval;
			tick = -offset;
			this.maxAge = maxAge;
			string = new ArrayList();
		}
		
		public void tick(Vector mousePos){
			if(tick%birthInterval == 0){
				PolarVector pVel = mousePos.getPolar();
				pVel.setD(rnd.rndUniform(8, 10));
				pVel.setA(pVel.getA() + rnd.rndUniform(-PI/2f, PI/2f));
				pVel.setE(pVel.getE() + rnd.rndUniform(-PI/2f, PI/2f));
				string.add(new Particle(mousePos, pVel.getCartesian(), 0.4f));
			}
			tick++;
			
		}
		
		public void draw(PApplet pa){
			Iterator si = string.iterator();
			int[] removeMarkers = new int[string.size()];
			int numMarkers = 0;
			int index = 0;
			Vector prev = null;
			while(si.hasNext()){
				Particle p = (Particle)si.next();
				p.tick();
				if(p.age >= maxAge){
					removeMarkers[numMarkers] = index;
					numMarkers++;
				}
				//else if (false){
				else if(p.pos.absLength() < 120){
					removeMarkers[numMarkers] = index;
					numMarkers++;
					//if(si.hasNext()){
					//	prev = p.pos;
					//	p = (Particle)si.next();
					//	drawImpact(pa, p, prev);
					//}
				}
				else{
					if(prev != null){
						pa.noFill();
						float stroke = 6f * (p.pos.z + prev.z) / (2f * 400f);
						if(stroke < 0) stroke = 0;
						//System.out.println(stroke);
						pa.strokeWeight( stroke);
						pa.stroke(255, (maxAge - p.age) * 200f / maxAge);
						pa.line(prev.x, prev.y, prev.z, p.pos.x, p.pos.y, p.pos.z);
					}		
					prev = p.pos;
				}
				
			}
			for(int i = 0; i < numMarkers; i++){
				string.remove(removeMarkers[i]);
			}
		}

		
	}
	
	public class Particle{
		Vector pos;
		Vector vel;
		float drag;
		int age;
		public Particle(Vector pos, Vector vel, float drag){
			this.pos = pos;
			this.vel = vel;
			age = 0;

		}
		
		public void tick(){
			float dist  = pos.absLength();
			vel = vel.mult(1 - drag).add(pos.scaleLengthTo(-50000f / (dist*dist)));
			pos = pos.add(vel);
			age++;
		}
		
		public void draw(PApplet pa, float alpha){
			pa.vertex(pos.x, pos.y, pos.z);
		}
		
	}

}
